Repository navigation
Allow show to write the screenshot to a file-like object - #2136
Open
lucas-santos-f wants to merge 1 commit into
Open
lucas-santos-f wants to merge 1 commit into
lucas-santos-f wants to merge 1 commit into
Conversation
screenshot now also takes a binary file-like object such as io.BytesIO. The PNG is then written to memory with vtkPNGWriter.WriteToMemoryOn and copied into it, which avoids a temporary file when the image is only needed in memory (docs, notebooks). Fixes CadQuery#1760
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2136 +/- ##
=======================================
Coverage 95.81% 95.81%
=======================================
Files 30 30
Lines 9612 9616 +4
Branches 1446 1447 +1
=======================================
+ Hits 9210 9214 +4
Misses 247 247
Partials 155 155 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1760
show(..., screenshot=...)now also takes a binary file-like object such asio.BytesIO. In that casevtkPNGWriterwrites to memory (WriteToMemoryOn) and the PNG bytes are written to the object, so docs or notebooks can get the image without going through a temporary file:A file name works as before. The check is
hasattr(screenshot, "write")rather thanisinstance(screenshot, str)because VTK also takes apathlib.PathinSetFileName, and that should keep working. The example above is added to the Screenshots section ofdoc/vis.rst.The new
test_screenshot_to_memoryuses thepatch_vtkfixture, but feeds the realvtkPNGWritera 10x5 image fromvtkImageCanvasSource2Din place of the window, and checks the PNG signature and the size in the IHDR chunk. On master it fails withTypeError: SetFileName argument 1.Checked locally (Windows 10, Python 3.12, pip install with cadquery-ocp 8.0.1.1, VTK 9.6.2):
BytesIOare identical to the file written withscreenshot="img.png"pytest -n 8: 904 passed, 3 skipped (master: 903 passed, 3 skipped), no new warningsblack . --diff --check(cq fork): nothing to reformatmypy cadquery: same errors as master; here they are almost allimport-untypedfor OCP, since the pip package has no stubsHappy to adjust anything if needed, e.g. if you'd rather have
showreturn the bytes.AI disclosure: this change was written with the help of Claude Code (Claude Opus 5.5).